package by.demchenko.sample.webapp.page.home.share;

import by.demchenko.sample.datamodel.Schedule;
import by.demchenko.sample.services.CourseService;
import by.demchenko.sample.services.UserService;
import by.demchenko.sample.webapp.app.BasicAuthenticationSession;
import by.demchenko.sample.webapp.page.home.BaseLayout;
import by.demchenko.sample.webapp.page.home.cards.CourseCard;
import by.demchenko.sample.webapp.page.home.cards.TeacherCard;
import by.demchenko.sample.webapp.page.home.panel.StudentCoursesPanel;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.AbstractItem;
import org.apache.wicket.markup.repeater.RepeatingView;

import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.List;

public class CoursesPage extends BaseLayout {

    @Inject
    CourseService courseService;

    @Inject
    UserService userService;

    public CoursesPage() {
        RepeatingView repeating = new RepeatingView("repeating");
        add(repeating);

        final List<Schedule> courses = courseService.getAllCourseRegistrations();
        for (Schedule schedule : courses) {
            AbstractItem item = new AbstractItem(repeating.newChildId());
            repeating.add(item);

            Link linkCourse = new Link("course") {
                @Override
                public void onClick() {
                    setResponsePage(new CourseCard(schedule.getCourse()));
                }
            };
            linkCourse.add(new Label("course_text", schedule.getCourse().getName()));
            item.add(linkCourse);

            Link linkTeacher = new Link("teacher") {
                @Override
                public void onClick() {
                    setResponsePage(new TeacherCard(schedule.getCourse().getTeacher()));
                }
            };
            linkTeacher.add(new Label("teacher_text", schedule.getCourse().getTeacher().getFullName(getSession().getLocale())));
            item.add(linkTeacher);

            Link link = new Link("registrations") {
                @Override
                public void onClick() {
                    setResponsePage(new RegistrationPage(schedule.getCourse(), schedule.getStartDate()));
                }
            };

            item.add(new Label("vacant", schedule.getTotal() - schedule.getRegistered()));

            if (!AuthenticatedWebSession.get().isSignedIn()) {
                link.setEnabled(false);
            } else {
                if (userService.isEnrolledInCourse(BasicAuthenticationSession.get().getUser(), schedule.getCourse(), schedule.getStartDate())) {
                    link.setEnabled(false);
                }
            }

            item.add(link);
            SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy");
            link.add(new Label("registrations_text", DATE_FORMAT.format(schedule.getStartDate())));
        }

        StudentCoursesPanel studentCourses = new StudentCoursesPanel("studentCourses", BasicAuthenticationSession.get().getUser());
        add(studentCourses);
        if (!AuthenticatedWebSession.get().isSignedIn()) {
            studentCourses.setVisible(false);
        }
    }
}
